package com.lql.lqlreader.login.ui

import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.widget.Toast
import com.google.gson.Gson
import com.lql.lqlreader.BaseActivity
import com.lql.lqlreader.LoginSuccessEvent
import com.lql.lqlreader.R
import com.lql.lqlreader.login.model.User
import com.lql.lqlreader.login.model.postLogin
import com.lql.lqlreader.service.subscriber.HttpResultSubscriber
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.nav_header.*
import kotlinx.android.synthetic.main.nav_header.btn_login
import org.greenrobot.eventbus.EventBus
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class LoginActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        setLeftTitleAndDisplayHomeAsUp("登录")

        btn_login.setOnClickListener {
            val isValid = loginInfoIsValid(et_username.text, et_password.text)

            if (!isValid) return@setOnClickListener

            postLogin(et_username.text.toString(), et_password.text.toString())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : HttpResultSubscriber<User>() {
                    override fun onSuccess(data: User?) { // 登录成功逻辑处理
                        val defaultSP = getSharedPreferences("loginInfo", MODE_PRIVATE)
                        defaultSP.edit().putString("loginUser", Gson().toJson(data)).apply()
                        Toast.makeText(this@LoginActivity, "登录成功", Toast.LENGTH_SHORT).show()
                        finish()
                        EventBus.getDefault().post(LoginSuccessEvent())
                    }

                    override fun _onError(status: Int, msg: String?) {
                        Toast.makeText(this@LoginActivity, msg, Toast.LENGTH_SHORT).show()
                    }
                })
        }

        tv_register.setOnClickListener {
            startActivity(Intent(this, RegisterActivity::class.java))
        }

    }

    private fun loginInfoIsValid(username: Editable, password: Editable): Boolean {
        val userNameText = username.toString()
        val passwordText = password.toString()

        if (TextUtils.isEmpty(userNameText)) {
            Toast.makeText(this, "请输入用户名或手机号", Toast.LENGTH_SHORT).show()
            return false
        }

        if (TextUtils.isEmpty(passwordText)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show()
            return false
        }

        if (userNameText.length < 6 || passwordText.length < 6) {
            Toast.makeText(this, "输入应不小于6字符", Toast.LENGTH_SHORT).show()
            return false
        }
        return true
    }

}